package com.siyoumi.app.modules.app_book.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siyoumi.app.entity.BookOrder;
import com.siyoumi.app.modules.app_book.entity.EnumBookOrderHandle;
import com.siyoumi.app.modules.app_book.entity.EnumBookOrderShareSalesHandle;
import com.siyoumi.app.modules.app_book.entity.EnumBookOrderStatus;
import com.siyoumi.app.modules.app_book.service.SvcBookOrder;
import com.siyoumi.component.XBean;
import com.siyoumi.component.XEnumBase;
import com.siyoumi.component.http.InputData;
import com.siyoumi.controller.AdminApiController;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.util.XDate;
import com.siyoumi.util.XReturn;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/xadmin/app_book/app_book__order_sale__list")
public class app_book__order_sale__list
        extends AdminApiController {
    @GetMapping()
    public XReturn index() {
        setPageTitle("订单列表");

        InputData inputData = InputData.fromRequest();
        inputData.dateSearch(this, "create_begin", "create_end");

        String[] select = {
                "border_id",
                "border_create_date",
                "border_date_begin",
                "border_date_end",
                "border_date_end_final",
                "border_user_name",
                "border_user_phone",
                "border_day",
                "border_set_name",
                "border_spu_count",
                "border_sku_user_count",
                "border_set_user_count",
                "border_deposit",
                "border_deposit_price",
                "border_price_spu",
                "border_price_final",
                "border_price_pay",
                "border_price_pay01",
                "border_price_pay_total",
                "border_status",
                "border_share_store_price",
                "border_share_sales_uid",
                "border_share_sales_price",
                "border_share_sales_price_use",
                "border_share_sales_level",
                "border_share_sales_handle",
                "border_share_sales_handle_date",
                "border_share_sales_handle_index",
                "border_refund",
                "border_handle",
                "border_handle_date",
                "border_spu_name",
                "border_spu_pay",
                "border_store_name",
                "border_share_sales_name",
                "border_edit",
                "border_continue",
                "border_continue_main_id",
                "border_continue_price_total",
                "border_continue_day_total",
        };
        JoinWrapperPlus<BookOrder> query = SvcBookOrder.getBean().listQuery(inputData);
        //query.join(BookSpu.table(), BookSpu.tableKey(), "border_spu_id");
        //query.join(BookStore.table(), BookStore.tableKey(), "border_store_id");
        query.eq("border_enable", 1)
                .between("border_status", 5, 20);
        query.orderByDesc("border_create_date")
                .orderByAsc("border_id");
        query.select(select);

        IPage<BookOrder> page = new Page<>(inputData.getPageIndex(), inputData.getPageSize(), !isAdminExport());
        //list
        IPage<Map<String, Object>> pageData = SvcBookOrder.getApp().getMaps(page, query);
        List<Map<String, Object>> list = pageData.getRecords();
        long count = pageData.getTotal();

        EnumBookOrderStatus enumOrderStatus = EnumBookOrderStatus.of(false);
        EnumBookOrderHandle enumOrderHandle = XEnumBase.of(EnumBookOrderHandle.class);
        //Integer todayNum = XDate.toNum(XDate.today());
        //分帐处理状态
        EnumBookOrderShareSalesHandle enumOrderShareSalesHandle = XEnumBase.of(EnumBookOrderShareSalesHandle.class);
        for (Map<String, Object> item : list) {
            BookOrder entity = XBean.fromMap(item, BookOrder.class);
            item.put("id", entity.getKey());
            //订单日期
            item.put("date_begin", XDate.toDateString(entity.getBorder_date_begin()));
            item.put("date_end", entity.strEndDate(false));
            item.put("date_end_continue", entity.strEndDate(true));
            //状态
            item.put("status", enumOrderStatus.get(entity.getBorder_status()));
            //处理状态
            item.put("handle", enumOrderHandle.get(entity.getBorder_handle()));
            //主播分帐处理状态
            String shareSalesHandle = enumOrderShareSalesHandle.get(entity.getBorder_share_sales_handle());
            item.put("share_sales_handle", shareSalesHandle);
        }

        getR().setData("list", list);
        getR().setData("count", count);
        //分帐状态
        setPageInfo("order_handle", enumOrderHandle);
        //主播分帐状态
        setPageInfo("order_share_sales_handle", enumOrderShareSalesHandle);
        //订单状态
        setPageInfo("order_status", EnumBookOrderStatus.of(true));

        return getR();
    }
}
